package lk.icta.core.dao.impl;

import lk.icta.common.dao.impl.BaseDAO;
import lk.icta.core.dao.GOInterestInformationDAO;
import lk.icta.core.entity.GOInterestInformation;
import lk.icta.user.entity.User;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.List;

/**
 * * @author Tharanga Punchihewa
 */
public class GOInterestInformationDAOImpl extends BaseDAO implements GOInterestInformationDAO {


    @Transactional(propagation = Propagation.MANDATORY)
    public void addGOInterestInformation(GOInterestInformation goInterestInformation, User user) {
        logger.debug("Adding a GO Information by : {}", user.getUsername());
        em.persist(goInterestInformation);
    }

    @Transactional(propagation = Propagation.MANDATORY)
    public void updateGOInterestInformation(GOInterestInformation goInterestInformation, User user) {
        logger.debug("Adding a GO Information by : {}", user.getUsername());
        em.merge(goInterestInformation);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public GOInterestInformation getGOInterestInformationById(long goInterestInformationId) {
        return em.find(GOInterestInformation.class, goInterestInformationId);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public List<GOInterestInformation> getGOInterestInformationListByState(GOInterestInformation.State state) {
        Query q = em.createNamedQuery("getGOInterestInformationListByState");
        q.setParameter("status", state);
        List result = q.getResultList();
        return result;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public List<GOInterestInformation> getGOInterestInformationListToBeEdited(User user) {
        Query q = em.createNamedQuery("getGOInterestInformationListToBeEdited");
        List result = q.getResultList();
        return result;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public List<GOInterestInformation> getGOInterestInformationList(User user) {
        Query q = em.createNamedQuery("getGOInterestInformationList");
        List result = q.getResultList();
        return result;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public List<GOInterestInformation> getGOInterestInformationListByOrganizationId(User user) {
        Query q = em.createNamedQuery("getGOInterestInformationListByOrganizationId");
        q.setParameter("organizationId", user.getOrganization().getId());
        List result = q.getResultList();
        return result;
    }
}